www.gusucode.com > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序 > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序\code\PhManage\DoctorDlg.cpp

    //Download by http://www.NewXing.com
// DoctorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PhysicM.h"
#include "DoctorDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDoctorDlg dialog


CDoctorDlg::CDoctorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDoctorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDoctorDlg)
	m_demo = _T("");
	m_name = _T("");
	//}}AFX_DATA_INIT
}


void CDoctorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDoctorDlg)
	DDX_Control(pDX, IDC_PHYSIC_LIST, m_List);
	DDX_Control(pDX, IDC_OFFICE_COMB, m_OfficeComb);
	DDX_Control(pDX, IDC_HOSPITAL_COMB, m_HospitalComb);
	DDX_Control(pDX, IDC_DUTY_COMB, m_DutyComb);
	DDX_Text(pDX, IDC_DEMO_EDIT, m_demo);
	DDX_Text(pDX, IDC_DOCTOR_NAME, m_name);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDoctorDlg, CDialog)
	//{{AFX_MSG_MAP(CDoctorDlg)
	ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton)
	ON_BN_CLICKED(IDC_DELETE_BUTTON, OnDeleteButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDoctorDlg message handlers

BOOL CDoctorDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_List.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
	char *head[]={"序 号","医生名称","所属医院","所属科室","职务","医生ID","备注"};
	int i;
	for (i=0;i<7;i++)
		m_List.InsertColumn(i,head[i],LVCFMT_LEFT,(i<2) ? 80 : 150,i);

	ImageList=new CImageList();
	ImageList->Create(16,16,TRUE | ILC_COLOR32,2,0);
	ImageList->Add(AfxGetApp()->LoadIcon(IDI_ICONUSER));
	ImageList->Add(AfxGetApp()->LoadIcon(IDR_MENUVIEW_TMPL));
	m_List.SetImageList(ImageList,LVSIL_SMALL);// LVSIL_NORMAL);
	Init();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void CDoctorDlg::Init()
{
	DBLibrary DB(((CPhysicMApp *)AfxGetApp())->DBSession);
	try{
		DB.Open("SELECT Hospital_id hid,Hospital_name hn from Hospital order by Hospital_id");
		int id=0;
		char pname[40],tmp[50];
		memset(pname,0,40);
		memset(tmp,0,50);
		while(!DB.isEof())
		{
			id=DB.GetValue("hid");
			DB.GetValue("hn",pname);
			sprintf(tmp,"[%03d]%s",id,pname);
			m_HospitalComb.AddString((LPCTSTR)tmp);
			DB.Next();
		}
		//
		DB.Open("SELECT Office_id oid,Office_name fn from Office order by Office_id");
		id=0;
		memset(pname,0,40);
		memset(tmp,0,50);
		while(!DB.isEof())
		{
			id=DB.GetValue("oid");
			DB.GetValue("fn",pname);
			sprintf(tmp,"[%03d]%s",id,pname);
			m_OfficeComb.AddString((LPCTSTR)tmp);
			DB.Next();
		}
//
		DB.Open("SELECT Duty_id did,Duty_name dn from Duty order by Duty_id");
		id=0;
		memset(pname,0,40);
		memset(tmp,0,50);
		while(!DB.isEof())
		{
			id=DB.GetValue("did");
			DB.GetValue("dn",pname);
			sprintf(tmp,"[%03d]%s",id,pname);
			m_DutyComb.AddString((LPCTSTR)tmp);
			DB.Next();
		}

		DB.Open("select a.doctor_id did,"
			" a.doctor_name dn,"
			" b.hospital_name hn,"
			" c.office_name fn,"
			" d.duty_name un,"
			" a.demo dm"
			" from doctor a,hospital b,office c,duty d "
			" where a.hospital_id=b.hospital_id and a.office_id=c.office_id and a.duty_id=d.duty_id"
			" order by a.doctor_id");
		int no=0;
		memset(tmp,0,50);
		while(!DB.isEof())
		{
			sprintf(tmp,"%03d",no+1);
			m_List.InsertItem(no,tmp,0);
			DB.GetValue("dn",tmp);
			m_List.SetItemText(no,1,tmp);
			DB.GetValue("hn",tmp);
			m_List.SetItemText(no,2,(LPCTSTR)tmp);
			DB.GetValue("fn",tmp);
			m_List.SetItemText(no,3,(LPCTSTR)tmp);
			DB.GetValue("un",tmp);
			m_List.SetItemText(no,4,(LPCTSTR)tmp);
			id=DB.GetValue("did");
			sprintf(tmp,"%03d",id);
			m_List.SetItemText(no,5,(LPCTSTR)tmp);
			DB.GetValue("dm",tmp);
			m_List.SetItemText(no,6,(LPCTSTR)tmp);
			//memset(tmp,0,50);
			no++;
			DB.Next();
		}
		
	}catch (DBErr &err)
	{
		char *str;
		int code;
		err.GetLastErr(code,&str);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("%s(%d)",str,code);
	}
	catch (...)
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("unknow err(%d)",-1);
	}
}
BOOL CDoctorDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	if (ImageList) delete ImageList;
	return CDialog::DestroyWindow();
}

void CDoctorDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}
void CDoctorDlg::AddInfo(CString m_pname,CString m_hp,CString m_of,CString m_du,CString m_dm)
{
	DBLibrary DB(((CPhysicMApp *)AfxGetApp())->DBSession);
	int max_id=0;
	CString hp_id,of_id,du_id;
	hp_id=m_hp.Mid(1,3);
	of_id=m_of.Mid(1,3);
	du_id=m_du.Mid(1,3);
	try{
		DB.Open("select IDENT_CURRENT('doctor') did "
			" from INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='doctor' ");
		double id=0;
		DB.GetValue("did",&id);
		max_id=(int)id+1;
		DB.ExecSQL("INSERT INTO doctor (doctor_name,hospital_id,office_id,duty_id,demo) "
			"VALUES('%s',%s,%s,%s,'%s')",m_pname,hp_id,of_id,du_id,m_dm);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddLog("新增医生:%s",m_pname);
		char no[4];
		int icount=m_List.GetItemCount();
		memset(no,0,4);
		sprintf(no,"%03d",icount+1);
		m_List.InsertItem(m_List.GetItemCount(),no,0);
		m_List.SetItemText(icount,1,(LPCTSTR)m_pname);
		m_List.SetItemText(icount,2,(LPCTSTR)m_hp);
		m_List.SetItemText(icount,3,(LPCTSTR)m_of);
		m_List.SetItemText(icount,4,(LPCTSTR)m_du);
		sprintf(no,"%03d",max_id);
		m_List.SetItemText(icount,5,(LPCTSTR)no);
		m_List.SetItemText(icount,6,(LPCTSTR)m_dm);

	}
	catch (DBErr &err)
	{
		char *str;
		int code;
		err.GetLastErr(code,&str);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("%s(%d)",str,code);
	}
	catch(...)
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("unknow err(%d)",-1);
	}
}

void CDoctorDlg::OnAddButton() 
{
	UpdateData();
	m_name.TrimLeft();
	m_name.TrimRight();
	if(m_name.IsEmpty())
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("医生名称不能为空,请输入");
		return;
	}

	CString hp,of,du;
	m_HospitalComb.GetWindowText(hp);
	m_OfficeComb.GetWindowText(of);
	m_DutyComb.GetWindowText(du);
	hp.TrimLeft();
	hp.TrimRight();
	of.TrimLeft();
	of.TrimRight();
	du.TrimLeft();
	du.TrimRight();
	if(hp.IsEmpty())
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("医院不能为空,请选择");
		return;
	}
	if(of.IsEmpty())
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("科室不能为空,请选择");
		return;
	}
	if(du.IsEmpty())
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("职务不能为空,请选择");
		return;
	}
	
	AddInfo(m_name,hp,of,du,m_demo);	
}

void CDoctorDlg::OnDeleteButton() 
{
   	((CPhysicMApp *)AfxGetApp())->Delete("doctor","doctor_id",&m_List,5);
}